﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceKinect
{
    class Bullet:GameObject
    {
        Texture2D texture;
        float radiusSphere = 0.05f;
        Vector3 directVector;
        float fireSpeedUFO = 2.0f;

        Random random = new Random();

        public Bullet (Texture2D t, Vector3 p, Quaternion q)
        {
            texture = t;
            position = p;
            rotation = q;
        }

        public Vector3 DirectVector
        {
            get { return directVector; }
            set { directVector = value; }
        }

        public void Update(float moveSpeed)
        {
            //moves bullet at 5 * ship speed
            MoveForward(moveSpeed * 10.0f);
            bound = new BoundingSphere(position, radiusSphere);
        }

        public void UpdateUFOBullet(float moveSpeed, Player player, float UFOFireSpeed)
        {      
            directVector.Normalize();
            position += directVector * moveSpeed * fireSpeedUFO;
            bound = new BoundingSphere(position, radiusSphere);
        }

        public CollisionType CheckCollision(List<UFO> UFOList, List<Target> targetList, BoundingBox mapBox, Model targetModel, Player player, Difficulty diff)
        {
            //this method is now only used by bullets, and determines whether a bullet has collided with a boundary wall, a target or a UFO

            //if boundary wall
            if (mapBox.Contains(bound) != ContainmentType.Contains)
                return CollisionType.Boundary;

            //if UFO
            for (int i = 0; i < UFOList.Count; i++)
            {
                if (UFOList[i].Bound.Contains(bound) != ContainmentType.Disjoint)
                {
                    UFOList[i].Health -= diff.playerUFOBulletDamage;
                    return CollisionType.UFO;
                }
            }

            //if target
            for (int i = 0; i < targetList.Count; i++)
            {
                if (targetList[i].Bound.Contains(bound) != ContainmentType.Disjoint)
                {
                    Target destroyedTarget = targetList[i];
                    player.TmpPowerUpLocation = targetList[i].Position;

                    //remove target
                    targetList.RemoveAt(i);
                    i--;

                    //add cluster in its place (if applicable)
                    AddCluster(destroyedTarget.Radius, destroyedTarget.Position, targetModel, mapBox, targetList);
                    
                    player.TargetScore += 10;

                    if (player.LavaCannon == false)
                        player.DestroyedTargets++;

                    player.ExplosVec = (destroyedTarget.Bound.Center - player.Position);
                    player.Explos = true;

                    return CollisionType.Target;
                }
            }
            return CollisionType.None;
        }

        public CollisionType CheckHit(Player player, List<Target> targetList)
        {
            //check if UFO bullet hits player
            if (player.Bound.Contains(bound) != ContainmentType.Disjoint)
                return CollisionType.Player;

            for (int i = 0; i < targetList.Count; i++)
            {
                if (targetList[i].Bound.Contains(bound) != ContainmentType.Disjoint)
                {
                    //Remove target? Not sure
                    return CollisionType.Target;
                }
            }

            return CollisionType.None;
        }

        public void AddCluster(float bigRad, Vector3 bigVec, Model targetModel, BoundingBox mapBox, List<Target> targetList)
        {
            float radius = 0;

            while (bigRad > .3)
            {
                radius = (float)random.NextDouble() / 4;
                float x = bigVec.X + ((float)random.NextDouble() * 2 - 1f);
                float y = bigVec.Y + ((float)random.NextDouble() * 2 - 1f);
                float z = bigVec.Z + ((float)random.NextDouble() * 2 - 1f);

                Target newTarget = new Target(targetModel, new Vector3(x, y, z), radius);

                if (newTarget.CheckTargetSetUpCollision(mapBox, targetList) == CollisionType.None)
                {
                    targetList.Add(newTarget);
                    bigRad -= radius;
                }
            }
        }
    }
}
